package edu.chl.dat255.bluebanana.presentation.impl;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.SeekBar;
import android.widget.Spinner;
import edu.chl.dat255.bluebanana.R;
import edu.chl.dat255.bluebanana.data.impl.DbConst;
import edu.chl.dat255.bluebanana.presentation.IScreenProfileSettings;
import edu.chl.dat255.bluebanana.utils.DatabaseUtil;
import edu.chl.dat255.bluebanana.utils.StringUtil;

public class ScreenProfileSettings extends Activity implements
		IScreenProfileSettings {
	private SeekBar brightnessSeekBar;
	private Spinner timeoutSpinner;
	private long profileId;
	private ArrayAdapter<CharSequence> adapter;
	private String[] array;
	private CheckBox autoBrightness;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.screensettings);

		array = getResources().getStringArray(R.array.timeoutTimes);

		profileId = getIntent().getLongExtra("Profile", -1);

		brightnessSeekBar = (SeekBar) findViewById(R.id.brightnessSeekBar);
		brightnessSeekBar.setMax(255);

		autoBrightness = (CheckBox) findViewById(R.id.autoBrightButton);
		autoBrightness.setOnCheckedChangeListener(this);

		timeoutSpinner = (Spinner) findViewById(R.id.timeoutSpinner);

		adapter = ArrayAdapter.createFromResource(this, R.array.timeoutTimes,
				android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

		timeoutSpinner.setAdapter(adapter);
	}

	@Override
	protected void onResume() {
		super.onResume();
		autoBrightness.setChecked(DatabaseUtil.getBooleanValue(profileId,
				DbConst.KEY_SCREEN_AUTO_BRIGHTNESS_ENABLE,
				ScreenProfileSettings.this));
		brightnessSeekBar.setEnabled(!DatabaseUtil.getBooleanValue(profileId,
				DbConst.KEY_SCREEN_AUTO_BRIGHTNESS_ENABLE,
				ScreenProfileSettings.this));

		timeoutSpinner.setSelection(getPosOfValue(DatabaseUtil.getIntValue(
				profileId, DbConst.KEY_SCREEN_TIMEOUT,
				ScreenProfileSettings.this)));
		brightnessSeekBar.setProgress(DatabaseUtil.getIntValue(profileId,
				DbConst.KEY_SCREEN_BRIGHTNESS, ScreenProfileSettings.this));
	}

	@Override
	protected void onPause() {
		super.onPause();
		// Minimal brightness level, should not be below 20.
		int progress = brightnessSeekBar.getProgress();
		if (progress < 20) {
			progress = 20;
		}
		DatabaseUtil.sendValueToDatabase(profileId,
				DbConst.KEY_SCREEN_BRIGHTNESS, progress,
				ScreenProfileSettings.this);

		int value = StringUtil.getOnlyNumerics(array[timeoutSpinner
				.getSelectedItemPosition()]);
		DatabaseUtil.sendValueToDatabase(profileId, DbConst.KEY_SCREEN_TIMEOUT,
				value, ScreenProfileSettings.this);

		DatabaseUtil.sendValueToDatabase(profileId,
				DbConst.KEY_SCREEN_AUTO_BRIGHTNESS_ENABLE,
				autoBrightness.isChecked(), ScreenProfileSettings.this);
	}

	private int getPosOfValue(int value) {
		int pos = -1;
		for (int i = 0; i < array.length; i++) {
			if (value == StringUtil.getOnlyNumerics(array[i])) {
				pos = i;
				break;
			}
		}
		return pos;
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		if (buttonView.equals(autoBrightness)) {
			brightnessSeekBar.setEnabled(!isChecked);
		}
	}
}
